﻿Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos DAL que implementa el patrón Table Data Gateway para el manejo de la entidad Publicacion
''' </summary>
''' <remarks></remarks>
Public Class TDGPublicacion

    Private Shared m_Instance As TDGPublicacion = Nothing


    Public Function Insert(conn As SqlConnection, _
                                            Busq_Numero As Int16, _
                                            MedB_Codigo As Int16, _
                                            Pub_FechaPubli As DateTime, _
                                            Pub_Detalle As String, _
                                            Optional Pub_Numero As Int16 = 0) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16

        Dim outParam_Pub_Numero As SqlParameter


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.addPublicacion"
        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)
        command.Parameters.AddWithValue("@MedB_Codigo", MedB_Codigo)
        command.Parameters.AddWithValue("@Pub_FechaPubli", Pub_FechaPubli)
        command.Parameters.AddWithValue("@Pub_Detalle", Pub_Detalle)

        outParam_Pub_Numero = command.Parameters.Add("@Pub_Numero", SqlDbType.SmallInt)
        outParam_Pub_Numero.Direction = ParameterDirection.InputOutput


        iRowsAffected = command.ExecuteNonQuery()


        ' Retorno valores
        Pub_Numero = outParam_Pub_Numero.Value

        Return iRowsAffected

    End Function

    Public Function Update(conn As SqlConnection, _
                                        Busq_Numero As Int16, _
                                        Pub_Numero As Int16, _
                                        MedB_Codigo As Int16, _
                                        Pub_FechaPubli As DateTime, _
                                        Pub_Detalle As String) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.updPublicacion"
        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)
        command.Parameters.AddWithValue("@Pub_Numero", Pub_Numero)
        command.Parameters.AddWithValue("@MedB_Codigo", MedB_Codigo)
        command.Parameters.AddWithValue("@Pub_FechaPubli", Pub_FechaPubli)
        command.Parameters.AddWithValue("@Pub_Detalle", Pub_Detalle)


        iRowsAffected = command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function Delete(conn As SqlConnection, Busq_Numero As Int16, Pub_Numero As Int16) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16

        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure
        command.CommandText = "dbo.delPublicacion"
        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)
        command.Parameters.AddWithValue("@Pub_Numero", Pub_Numero)

        iRowsAffected = command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function Find(conn As SqlConnection, Busq_Numero As Int16, Pub_Numero As Int16) As SqlDataReader
        Dim command As New SqlCommand
        Dim dr As SqlDataReader


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.getPublicacion"
        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)
        command.Parameters.AddWithValue("@Pub_Numero", Pub_Numero)


        dr = command.ExecuteReader()

        Return dr

    End Function

    Public Function FindAllPorBusqueda(conn As SqlConnection, Busq_Numero As Int16) As SqlDataReader
        Dim command As New SqlCommand
        Dim dr As SqlDataReader


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.[getPublicacion_PorBusqueda_All]"

        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)

        dr = command.ExecuteReader()

        Return dr

    End Function

    Public Shared ReadOnly Property Instance()
        Get
            If (m_Instance Is Nothing) Then
                m_Instance = New TDGPublicacion()
            End If

            Return m_Instance
        End Get
    End Property

End Class
